package com.peza.bikelog.gui.restclient;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.peza.bikelog.common.domain.LogEntry;
import com.peza.bikelog.common.domain.http.HttpProtocol;
import com.peza.bikelog.common.json.JsonService;

@Service
public class RestClientImpl implements RestClient{

	private Header acceptHeader = new Header("Accept", "application/json");
	private final static String CONTENT_TYPE = "application/x-www-form-urlencoded";
	private final static String CHARSET = "utf-8";
	private Header contentHeader = new Header("Content-type", CONTENT_TYPE+"; charset="+CHARSET);
	private static final String URL = "http://localhost:8090/bikelog/";
	private HttpClient httpClient;
	
	private static Logger logger = Logger.getLogger(RestClientImpl.class); 
	
	@Autowired
	private JsonService js;
	
	@Autowired
	RestTemplate restTemplate;
	
	public RestClientImpl (){
		httpClient = new HttpClient();
		restTemplate = new RestTemplate();
	}	
	
	@Override
	public LogEntry findOne(Long id){
		LogEntry entry = restTemplate.getForObject(URL+"entry/"+id, LogEntry.class);
		return entry;
	}
	
	@Override
	public List<LogEntry> findRange(long from, long to){
		String path = "entries/"+from+"/"+to;
		LogEntry[] list = restTemplate.getForObject(URL + path, LogEntry[].class);
		return Arrays.asList(list);
	}
	

	@Override
	public List<LogEntry> findAll() {
		String path = "entries/all";
		LogEntry[] list = restTemplate.getForObject(URL + path, LogEntry[].class);
		return Arrays.asList(list);
	}

	@Override
	public LogEntry update(LogEntry entry) {
		String path = "entry/update";
		LogEntry response = restTemplate.postForObject(URL + path, entry , LogEntry.class);
		return response;

	}

	
	@Override
	public void updateAll(Collection<LogEntry> entries) {
		String path = "entries/update";
		LogEntry[] request = (LogEntry[]) entries.toArray();
		restTemplate.postForObject(URL + path, request , LogEntry.class);
	}

	
	@Override
	public LogEntry create(LogEntry entry) {
		String path = "entry/save";
		restTemplate.put(URL+path, entry);
		return entry;
	}
	
	
	@Override
	public void delete(LogEntry entry) {
		String path = "entry/remove";
		restTemplate.delete(URL+path, entry);		
	}

	@Override
	public void deletAll(Collection<LogEntry> entries) {
		String path = "entries/remove";
		restTemplate.delete(URL+path, (LogEntry[])entries.toArray());
		
	}

	
	public static void main (String [] args){
		
		RestClientImpl client = new RestClientImpl();
	}


	
}
